```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>过滤器与拦截器详解 | Web开发核心技术</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.8;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background: #2d2d2d;
            border-radius: 8px;
            position: relative;
        }
        .code-block pre {
            overflow-x: auto;
            padding: 1.5rem;
            color: #f8f8f2;
            font-family: 'Consolas', 'Monaco', 'Andale Mono', monospace;
            line-height: 1.5;
            tab-size: 4;
        }
        .code-title {
            position: absolute;
            top: -12px;
            left: 20px;
            background: #6e8efb;
            color: white;
            padding: 4px 12px;
            border-radius: 4px;
            font-size: 0.85rem;
            font-weight: 500;
        }
        .feature-card {
            transition: all 0.3s ease;
            border-radius: 12px;
            overflow: hidden;
        }
        .feature-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .comparison-item {
            border-bottom: 1px solid #eee;
            transition: background 0.2s;
        }
        .comparison-item:hover {
            background: rgba(110, 142, 251, 0.05);
        }
        .diagram-container {
            background: white;
            border-radius: 12px;
            padding: 20px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .highlight {
            color: #6e8efb;
            font-weight: 600;
        }
        .first-letter {
            font-size: 3.5em;
            line-height: 0.8;
            float: left;
            margin-right: 8px;
            color: #a777e3;
            font-weight: 700;
        }
        .footer-link:hover {
            color: #a777e3 !important;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 md:py-32 px-6">
        <div class="max-w-5xl mx-auto text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6 leading-tight">过滤器与拦截器</h1>
            <p class="text-xl md:text-2xl font-light mb-8 opacity-90 max-w-3xl mx-auto">
                Web应用中的请求处理双雄：架构设计与最佳实践
            </p>
            <div class="flex justify-center space-x-4">
                <div class="bg-white bg-opacity-20 px-6 py-2 rounded-full text-sm font-medium">
                    <i class="fas fa-code mr-2"></i>Java Web开发
                </div>
                <div class="bg-white bg-opacity-20 px-6 py-2 rounded-full text-sm font-medium">
                    <i class="fas fa-project-diagram mr-2"></i>Spring MVC架构
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-5xl mx-auto px-6 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-12 h-1 bg-purple-500 rounded-full mr-4"></div>
                <h2 class="text-xl font-semibold text-gray-700">核心概念</h2>
            </div>
            <p class="text-gray-700 mb-6">
                <span class="first-letter">F</span>ilter和Interceptor是Web应用中常用的两种组件，它们用于在请求和响应的不同阶段执行特定的操作。虽然它们的作用有些相似，但在实现和使用上存在关键区别。理解这两种组件的特性和适用场景，对于构建灵活、高效的Web应用架构至关重要。本文将深入剖析它们的原理、实现方式以及在实际项目中的最佳实践。
            </p>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-8 mt-10">
                <div class="feature-card bg-white p-8 shadow-lg">
                    <div class="text-purple-500 text-4xl mb-4">
                        <i class="fas fa-filter"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3 text-gray-800">过滤器(Filter)</h3>
                    <p class="text-gray-600">
                        由Servlet容器管理，在请求进入Servlet容器之前和响应离开容器之前触发。可以对所有类型的请求进行统一处理，包括静态资源、Servlet和JSP等。
                    </p>
                </div>
                <div class="feature-card bg-white p-8 shadow-lg">
                    <div class="text-blue-500 text-4xl mb-4">
                        <i class="fas fa-shield-alt"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3 text-gray-800">拦截器(Interceptor)</h3>
                    <p class="text-gray-600">
                        由Spring框架管理，在请求进入Controller之前和返回视图渲染之前触发。专门用于处理Spring MVC请求，可以访问处理器的上下文。
                    </p>
                </div>
            </div>
        </section>

        <!-- Key Differences -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-1 bg-blue-500 rounded-full mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">核心区别对比</h2>
            </div>
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="grid grid-cols-5 bg-gray-100 p-4 font-medium text-gray-700">
                    <div class="col-span-2">对比维度</div>
                    <div class="text-center">过滤器(Filter)</div>
                    <div class="text-center">拦截器(Interceptor)</div>
                    <div class="text-center">图示说明</div>
                </div>
                <div class="divide-y divide-gray-200">
                    <div class="comparison-item grid grid-cols-5 p-4">
                        <div class="col-span-2 font-medium">触发时机</div>
                        <div>请求进入Servlet容器前和响应离开容器前</div>
                        <div>请求进入Controller前和返回视图渲染前</div>
                        <div class="text-center text-blue-500">
                            <i class="fas fa-clock"></i>
                        </div>
                    </div>
                    <div class="comparison-item grid grid-cols-5 p-4">
                        <div class="col-span-2 font-medium">触发方式</div>
                        <div>由Servlet容器触发</div>
                        <div>由Spring MVC框架触发</div>
                        <div class="text-center text-blue-500">
                            <i class="fas fa-bolt"></i>
                        </div>
                    </div>
                    <div class="comparison-item grid grid-cols-5 p-4">
                        <div class="col-span-2 font-medium">使用范围</div>
                        <div>所有请求(Servlet、JSP、静态资源等)</div>
                        <div>仅Spring MVC请求</div>
                        <div class="text-center text-blue-500">
                            <i class="fas fa-globe"></i>
                        </div>
                    </div>
                    <div class="comparison-item grid grid-cols-5 p-4">
                        <div class="col-span-2 font-medium">执行顺序</div>
                        <div>web.xml配置顺序决定</div>
                        <div>Spring拦截器链顺序决定</div>
                        <div class="text-center text-blue-500">
                            <i class="fas fa-sort-amount-down"></i>
                        </div>
                    </div>
                    <div class="comparison-item grid grid-cols-5 p-4">
                        <div class="col-span-2 font-medium">返回结果处理</div>
                        <div>不能修改响应内容</div>
                        <div>可修改ModelAndView对象</div>
                        <div class="text-center text-blue-500">
                            <i class="fas fa-edit"></i>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Code Examples -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-1 bg-green-500 rounded-full mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">代码实现对比</h2>
            </div>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
                <!-- Filter Code -->
                <div class="code-block">
                    <div class="code-title">过滤器实现</div>
                    <pre><code class="language-java">import javax.servlet.*;
import java.io.IOException;

public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig config) throws ServletException {
        // 初始化操作
    }

    @Override
    public void doFilter(ServletRequest request, 
                        ServletResponse response,
                        FilterChain chain)
            throws IOException, ServletException {
        // 请求预处理
        // ...

        // 调用后续过滤器或目标资源
        chain.doFilter(request, response);

        // 响应后处理
        // ...
    }

    @Override
    public void destroy() {
        // 销毁操作
    }
}</code></pre>
                </div>
                
                <!-- Interceptor Code -->
                <div class="code-block">
                    <div class="code-title">拦截器实现</div>
                    <pre><code class="language-java">import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.*;

public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request,
                           HttpServletResponse response,
                           Object handler) throws Exception {
        // Controller之前的预处理
        return true; // 继续执行请求
    }

    @Override
    public void postHandle(HttpServletRequest request,
                         HttpServletResponse response,
                         Object handler,
                         ModelAndView modelAndView) throws Exception {
        // Controller之后，视图渲染前的处理
    }

    @Override
    public void afterCompletion(HttpServletRequest request,
                              HttpServletResponse response,
                              Object handler,
                              Exception ex) throws Exception {
        // 视图渲染后的处理
    }
}</code></pre>
                </div>
            </div>
        </section>

        <!-- Execution Flow -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-1 bg-orange-500 rounded-full mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">执行流程图示</h2>
            </div>
            
            <div class="diagram-container mb-8">
                <div class="mermaid">
                    sequenceDiagram
                        participant Client
                        participant Tomcat
                        participant Filter
                        participant DispatcherServlet
                        participant Interceptor
                        participant Controller
                        
                        Client->>Tomcat: HTTP请求
                        Tomcat->>Filter: 请求预处理
                        Filter->>DispatcherServlet: 传递请求
                        DispatcherServlet->>Interceptor: preHandle()
                        Interceptor->>Controller: 执行Controller
                        Controller->>Interceptor: 返回结果
                        Interceptor->>DispatcherServlet: postHandle()
                        DispatcherServlet->>Filter: 响应返回
                        Filter->>Tomcat: 响应后处理
                        Tomcat->>Client: HTTP响应
                </div>
            </div>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
                <div>
                    <img src="https://cdn.nlark.com/yuque/0/2023/png/21449790/1689776581167-c9ecd651-089f-435f-871c-d7bb02915a6e.png" 
                         alt="Filter和Interceptor执行流程" 
                         class="rounded-lg shadow-md w-full h-auto">
                </div>
                <div>
                    <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1713513768031-8f5fddbc-5646-447c-b666-2ca5884d39a3.png" 
                         alt="Spring请求处理流程" 
                         class="rounded-lg shadow-md w-full h-auto">
                </div>
            </div>
        </section>

        <!-- Best Practices -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-1 bg-red-500 rounded-full mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">最佳实践指南</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-8">
                    <div class="mb-6">
                        <h3 class="text-lg font-semibold text-gray-800 mb-3 flex items-center">
                            <span class="w-6 h-6 bg-red-500 rounded-full text-white flex items-center justify-center mr-3 text-sm">1</span>
                            适用场景选择
                        </h3>
                        <ul class="list-disc pl-6 text-gray-600 space-y-2">
                            <li>使用<span class="highlight">过滤器(Filter)</span>处理与框架无关的通用功能：字符编码设置、跨域处理、XSS防护、请求日志记录等</li>
                            <li>使用<span class="highlight">拦截器(Interceptor)</span>处理与业务逻辑相关的功能：权限验证、参数预处理、响应统一包装、业务日志等</li>
                        </ul>
                    </div>
                    
                    <div class="mb-6">
                        <h3 class="text-lg font-semibold text-gray-800 mb-3 flex items-center">
                            <span class="w-6 h-6 bg-red-500 rounded-full text-white flex items-center justify-center mr-3 text-sm">2</span>
                            性能优化建议
                        </h3>
                        <ul class="list-disc pl-6 text-gray-600 space-y-2">
                            <li>过滤器中避免复杂的业务逻辑，保持轻量级</li>
                            <li>拦截器中的preHandle方法应快速返回，避免阻塞请求</li>
                            <li>合理设置过滤器和拦截器的执行顺序，减少不必要的处理</li>
                        </ul>
                    </div>
                    
                    <div>
                        <h3 class="text-lg font-semibold text-gray-800 mb-3 flex items-center">
                            <span class="w-6 h-6 bg-red-500 rounded-full text-white flex items-center justify-center mr-3 text-sm">3</span>
                            组合使用策略
                        </h3>
                        <ul class="list-disc pl-6 text-gray-600 space-y-2">
                            <li>过滤器处理基础设施层功能</li>
                            <li>拦截器处理业务层功能</li>
                            <li>两者结合实现完整的请求处理链</li>
                            <li>利用各自的优势构建分层防御体系</li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12 px-6">
        <div class="max-w-5xl mx-auto">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-6 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                    <p class="text-gray-400">深入探索技术本质</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="footer-link text-gray-400 hover:text-white transition-colors">
                        <i class="fas fa-globe mr-2"></i>http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-sm text-gray-500">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        // Initialize Mermaid
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // Smooth scroll for anchor links
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>
```